#include "emaildialog.h"
#include "ui_emaildialog.h"
#include "zsmtp.h"

#include <QDebug>
#include <QMouseEvent>

#include <QGraphicsDropShadowEffect>
#include <QColor>
#include <QIntValidator>

#include <QMessageBox>

EmailDialog::EmailDialog(QWidget *parent, QString email) :
    QDialog(parent),
    ui(new Ui::EmailDialog),
    m_email(email)
{
    ui->setupUi(this);


    // 设置标题
    ui->label->setText("邮箱验证码对话框");


    // 限制只能输入数字

    QIntValidator *validator = new QIntValidator(0, 999999, ui->le_code);
    ui->le_code->setValidator(validator);

    // 隐藏主窗体边框
    this->setWindowFlag(Qt::FramelessWindowHint);
    this->setAttribute(Qt::WA_TranslucentBackground);

    // 鼠标拖动窗体
    m_areaMovable = geometry();
    m_bPressed = false;

    //设置具体阴影
    QGraphicsDropShadowEffect *shadow_effect = new QGraphicsDropShadowEffect(this);
    shadow_effect->setOffset(0, 0);
    //阴影颜色
    shadow_effect->setColor(QColor(38, 78, 119, 127));
    //阴影半径
    shadow_effect->setBlurRadius(22);
    ui->frame->setGraphicsEffect(shadow_effect);

    if(!m_email.isEmpty())
    {
        send_verification_code(m_email);
    }
}

EmailDialog::~EmailDialog()
{
    delete ui;
}


void EmailDialog::send_verification_code(QString email)
{
    // 输入的时候有邮箱后缀提示
    ZSmtp *smtp = new ZSmtp("smtp.163.com");

    m_randCodeStr = smtp->getRandStr(6);// 生成6位验证码

    connect(smtp, SIGNAL(disconnected()), smtp, SLOT(deleteLater()));
    connect(smtp, &ZSmtp::emailStatus, [=](int status){
        if(status == EMAIL_SUCCEED)	//邮件发送成功
        {
            //do something
            qDebug() << "邮件发送成功";

        }
        else if(status == EMAIL_ERROR)
        {//邮件发送失败（原因可能是发、收件人邮箱号写错了，smtp服务器崩了等）
            //do something
            qDebug() << "邮件发送失败";

        }
        ui->tb_enter->setEnabled(true);

    });
    QString subject="Qt智能家居控制系统";
    QString content="欢迎注册基于Qt的智能家居控制系统，本次操作的验证码为：" + m_randCodeStr;
    smtp->sendEmail("13272456305@163.com", "LCAQZWXNGNPHDELL", email, subject, content);

    ui->tb_enter->setEnabled(false);
}

void EmailDialog::on_tb_enter_clicked()
{
    QString code = ui->le_code->text().trimmed();

    if(code.isEmpty())
    {
        QMessageBox::warning(this, "提示", "验证码不能为空");
        return;
    }

    if(m_randCodeStr == code)
    {
        this->accept();
    }
    else
    {
        QMessageBox::warning(this, "错误", "验证码错误");
        return;
    }
}

void EmailDialog::on_tb_close_clicked()
{
    this->close();
}


// 鼠标点击事件
void EmailDialog::mousePressEvent(QMouseEvent *e)
{
    //鼠标左键
    if(e->button() == Qt::LeftButton)
    {
        m_ptPress = e->pos();
//        qDebug() << pos() << e->pos() << m_ptPress;
        m_bPressed = m_areaMovable.contains(m_ptPress);

    }
}

// 鼠标移动事件
void EmailDialog::mouseMoveEvent(QMouseEvent *e)
{
    if(m_bPressed)
    {
//        qDebug() << pos() << e->pos() << m_ptPress;
        move(pos() + e->pos() - m_ptPress);
    }
}

// 鼠标松手事件
void EmailDialog::mouseReleaseEvent(QMouseEvent *)
{
    m_bPressed = false;
}

//设置鼠标按下的区域
void EmailDialog::setAreaMovable(const QRect rt)
{

    if(m_areaMovable != rt)
    {
        m_areaMovable = rt;
    }
}

void EmailDialog::setTextHint(QString text)
{
    ui->label->setText(text);
}


